package web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.annotation.SuppressLint;
import android.os.StrictMode;

public class get_product_details {
	String Location;

	@SuppressLint("NewApi")
	public product getProduct_details(String Nameq) {

		String finalresponse;

	
		String urlname = Nameq.replaceAll(" ", "%20");

		HttpClient httpclient = new DefaultHttpClient();

		String Url = "http://wheresmymaggi.net78.net/getproductdetail.php?Nameq="
				+ urlname;

		HttpGet httpget = new HttpGet(Url);

		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
			.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}

		try {

			// Execute HTTP Post Request
			HttpResponse response = httpclient.execute(httpget);

			HttpEntity entity = response.getEntity();

			InputStream is = entity.getContent();

			finalresponse = convertStreamToString(is);

			RemoveExtraCode R1 = new RemoveExtraCode();
			finalresponse = R1.removeExtra(finalresponse);

			String[] responsearray = finalresponse.split(",");

			product p1 = new product();
			p1.setNo(Integer.parseInt(responsearray[0]));
			p1.setName(responsearray[1]);
			p1.setLattitude(responsearray[2]);
			p1.setLongitude(responsearray[3]);

			return p1;

		} catch (Exception E) {
			System.out.print(E.toString());

		} finally {

		}
		// return finalresponse;

		return null;

	}

	private static String convertStreamToString(InputStream is) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append((line + "\n"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

}
